package com.git.smp.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.git.smp.cache.DataDictionaryCache;
import com.git.smp.core.PageRequest;
import com.git.smp.core.ResponseMessage;
import com.git.smp.dao.DataDictionaryMapper;
import com.git.smp.entity.DataDictionary;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/dataDictionary")
public class DataDictionaryController {
	
	Logger logger = LoggerFactory.getLogger(DataDictionaryController.class);

	
	@Autowired
	DataDictionaryMapper dataDictionaryMapper;
	
	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/selectAll")
	public ResponseMessage<List<DataDictionary>> selectAll() {
		ResponseMessage<List<DataDictionary>> responseMessage = new ResponseMessage<List<DataDictionary>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询成功！");

		List<DataDictionary> dataDictionaryList = DataDictionaryCache.getDataDictionaryList();
		if (dataDictionaryList == null) {
			responseMessage.setCode("0001");
			
			responseMessage.setMessage("查询菜单信息失败！");
		}
		responseMessage.setExtend(dataDictionaryList);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/selectByPage")
	public ResponseMessage<PageInfo<DataDictionary>> selectByPage(@RequestBody PageRequest<DataDictionary> pageRequest) {
		ResponseMessage<PageInfo<DataDictionary>> responseMessage = new ResponseMessage<PageInfo<DataDictionary>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询成功！");

		DataDictionary dataDictionary = pageRequest.getBody();

		PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
		List<DataDictionary> listDataDictionary = dataDictionaryMapper.select(dataDictionary);
		if (listDataDictionary == null) {
			responseMessage.setCode("0001");
			
			responseMessage.setMessage("查询菜单信息失败！");
		}
		PageInfo<DataDictionary> pageInfo = new PageInfo<DataDictionary>(listDataDictionary, pageRequest.getPageSize());
		responseMessage.setExtend(pageInfo);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/adddataDictionary")
	public ResponseMessage adddataDictionary(@RequestBody DataDictionary dataDictionary) {
		ResponseMessage<PageInfo<DataDictionary>> responseMessage = new ResponseMessage<PageInfo<DataDictionary>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("用户新增成功！");
		try {
			dataDictionaryMapper.insertSelective(dataDictionary);
		} catch (Exception e) {
			logger.error("",e);
			responseMessage.setCode("0001");
			responseMessage.setMessage("用户新增失败！");
		}

		return responseMessage;
	}
	
	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/updatedataDictionary")
	public ResponseMessage updatedataDictionary(@RequestBody DataDictionary dataDictionary) {
		ResponseMessage<PageInfo<DataDictionary>> responseMessage = new ResponseMessage<PageInfo<DataDictionary>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("用户修改成功！");
		try {
			dataDictionaryMapper.updateByPrimaryKeySelective(dataDictionary);
		} catch (Exception e) {
			logger.error("",e);
			responseMessage.setCode("0001");
			responseMessage.setMessage("用户修改失败！");
		}

		return responseMessage;
	}
	
	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/deletedataDictionary")
	public ResponseMessage deletedataDictionary(@RequestBody DataDictionary dataDictionary) {
		ResponseMessage<PageInfo<DataDictionary>> responseMessage = new ResponseMessage<PageInfo<DataDictionary>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("用户修改成功！");
		try {
			dataDictionaryMapper.deleteByPrimaryKey(dataDictionary);
		} catch (Exception e) {
			logger.error("",e);
			responseMessage.setCode("0001");
			responseMessage.setMessage("用户修改失败！");
		}

		return responseMessage;
	}
	
	
}
